package com.example.administrator.boltshow;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.ListView;

import com.example.administrator.boltshow.viewone.SwipeBackLayout;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import adapter.CatIdBaseAdapter;
import config.NetConfig;
import model.BoltCatIdModel;

public class CatIDActivity extends AppCompatActivity {
    private PullToRefreshListView mListView;
    private Toolbar mToolbar;
    private List<BoltCatIdModel.DataBean> mData;
    private CatIdBaseAdapter mAdapter;
    private String mCatId;
    private String mCatName;
    private int mCurrentPosition = 1;
    private WebView mWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cat_id);
        //设置回调,然后就没然后了~~~
        SwipeBackLayout swipeBackLayoutTwo = (SwipeBackLayout) findViewById(R.id.swipe_layout_two);
        swipeBackLayoutTwo.setCallBack(new SwipeBackLayout.CallBack() {
            @Override
            public void onFinish() {
                finish();
            }
        });
        initView();
        initData();
        setData();
        setListener();
    }

    private void setListener() {
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //传linkid 过去
                Bundle bundle = new Bundle();
                bundle.putString("linkid",mData.get(position).getLinkId());
                Intent intent = new Intent(CatIDActivity.this, BoltDetailsActivity.class);
                intent.putExtra("bundle",bundle);
                startActivity(intent);
            }
        });
    }

    private void setData() {
        mListView.setMode(PullToRefreshBase.Mode.BOTH);
        mListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                mCurrentPosition=1;
                loadMore(mCurrentPosition);
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
               // loadMore(mCurrentPosition);

            }
        });
        // 通过代理来设置 上拉和下拉的提示信息
        ILoadingLayout loadingLayoutProxy = mListView.getLoadingLayoutProxy();
        loadingLayoutProxy.setReleaseLabel("手放开");
        loadingLayoutProxy.setRefreshingLabel("正在为您拼命加载");
        loadingLayoutProxy.setLastUpdatedLabel("上一次刷新的时候：一万前");
       // loadingLayoutProxy.setPullLabel("马上为您加载");
        //设置头布局
        View head = LayoutInflater.from(this).inflate(R.layout.view_head_catid, null);
        mWebView = (WebView) head.findViewById(R.id.wv_head_catid_show);
        ListView listView = mListView.getRefreshableView();
        listView.addHeaderView(head);
        mListView.setAdapter(mAdapter);

        mToolbar.setTitle(mCatName);
        mToolbar.setSubtitleTextColor(Color.BLACK);
    }

    private void initData() {
        mData = new ArrayList<>();
        mAdapter = new CatIdBaseAdapter(mData, this);
        //接收传过来的数据
        Intent intent = getIntent();
        Bundle bundle = intent.getBundleExtra("bundle");
        mCatId = bundle.getString("catId");
        Log.i("TAG", "mCatId111"+mCatId);
        mCatName = bundle.getString("catName");
        loadMore(mCurrentPosition);
    }

    private void initView() {
        mListView = (PullToRefreshListView) findViewById(R.id.lv_catid_show);
        mToolbar = (Toolbar) findViewById(R.id.tl_catid_toolbar);
    }

    public void loadMore(final int position) {
        String path = String.format(NetConfig.BOTL_SENCOND_PATH, Integer.parseInt(mCatId), position);
        x.http().get(new RequestParams(path), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (result != null) {
                    Gson gson = new Gson();
                    BoltCatIdModel boltCatIdModel = gson.fromJson(result, BoltCatIdModel.class);
                    mWebView.loadUrl(boltCatIdModel.getBrand().getBrandUrl());
                    mWebView.setWebChromeClient(new WebChromeClient());
                    mWebView.setWebViewClient(new WebViewClient());
                    if (position == 1) {
                        mData.clear();
                    }
                    mCurrentPosition++;
                    mData.addAll(boltCatIdModel.getData());
                    mAdapter.notifyDataSetChanged();
                    mListView.onRefreshComplete();
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }


}
